package com.dzl.supplier;

import com.alibaba.fastjson.JSONObject;
import com.dzl.base.controller.BaseController;
import com.dzl.constant.CommonConstants;
import com.dzl.supplier.pojo.WelfareSupplier;
import com.dzl.supplier.pojo.WelfareSupplierPaymentinfo;
import com.dzl.supplier.pojo.dto.WelfareSupplierDeliveryDto;
import com.dzl.supplier.pojo.dto.WelfareSupplierDto;
import com.dzl.supplier.pojo.dto.WelfareSupplierRestrictedAreasDto;
import com.dzl.supplier.pojo.query.WelfareSupplierQuery;
import com.dzl.supplier.pojo.vo.WelfareSupplierVo;
import com.dzl.supplier.service.WelfareSupplierPaymentinfoService;
import com.dzl.supplier.service.WelfareSupplierService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.dzl.util.PasswordUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 供应商信息表(WelfareSupplier)表控制层
 * @author caobucao
 * @since 2021-05-18 16:30:31
 */
@Api(value = "企业端-供应商信息管理", tags = "企业端-供应商信息管理")
@RestController
@RequestMapping("welfareSupplier")
public class WelfareSupplierController extends BaseController {

    @Autowired
    private WelfareSupplierService welfareSupplierService;
    @Autowired
    private WelfareSupplierPaymentinfoService welfareSupplierPaymentinfoService;

    /**
     * 分页查询供应商列表
     * @date 2021/5/18 16:39
     * @author caobucao
     * @param query 查询条件
     * @return com.dzl.util.PageUtils<com.dzl.supplier.entity.vo.WelfareSupplierVo>
     */
    @ApiOperation(value = "企业端-分页查询供应商列表", notes = "企业端-分页查询供应商列表")
    @GetMapping("querySupplierVoListPage")
    public PageUtils<WelfareSupplierVo> querySupplierVoListPage(WelfareSupplierQuery query) {
        if(getAdminInfo().getAppRuleConfig()==CommonConstants.APP_RULE_CONFIG.SYNC_PLAFORM){
            query.setEnterpriseId(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID);
        }else{
            query.setEnterpriseId(getAdminInfo().getEnterpriseId());
        }
        PageInfo<WelfareSupplierVo> pageInfo = welfareSupplierService.getSupplierVoListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 根据品牌id获取供应商分页列表信息
     * @date 2021/12/27 10:55
     * @author liuguanjun
     * @param query 查询条件
     */
    @GetMapping("selectSupplierListByBrandId")
    public JsonResult<List<WelfareSupplier>> selectSupplierListByBrandId(WelfareSupplierQuery query) {
        query.setEnterpriseId(getAdminInfo().getEnterpriseId());
        List<WelfareSupplier> list = welfareSupplierService.selectSupplierListByBrandId(query);
        return JsonResult.success(list);
    }


    /**
     * 保存供应商
     * @date 2021/5/18 16:55
     * @author caobucao
     * @param welfareSupplier 供应商对象
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @ApiOperation(value = "企业端-保存供应商", notes = "企业端-保存供应商")
    @PostMapping("saveSupplier")
    public JsonResult<Integer> saveSupplier(@RequestBody WelfareSupplierDto welfareSupplier) {
        if(EmptyUtils.isEmpty(welfareSupplier.getName())) {
            return JsonResult.paramErrWithMsg("请填写供应商名称");
        }
       /* if(EmptyUtils.isEmpty(welfareSupplier.getCode())) {
            return JsonResult.paramErrWithMsg("请填写供应商代码");
        }*/
        if(EmptyUtils.isEmpty(welfareSupplier.getDeliveryTime())) {
            return JsonResult.paramErrWithMsg("请填写发货时间");
        }
        if(null == welfareSupplier.getDeliveryDays()) {
            return JsonResult.paramErrWithMsg("请填写发货天数");
        }
        if(EmptyUtils.isNotEmpty(welfareSupplier.getId())) {
            //编辑无法更改isApi和merchantId
            welfareSupplier.setIsApi(null);
            welfareSupplier.setMerchantId(null);
            welfareSupplier.setUpdater(getAdminInfo().getId());
            welfareSupplier.setUpdaterName(getAdminInfo().getUserName());
            welfareSupplier.setUpdateTime(new Date());
        } else {
            /*if (welfareSupplier.getIsApi() == null){
                return JsonResult.paramErrWithMsg("是否接入API不能为空");
            }
            if (welfareSupplier.getIsApi() == 1 && EmptyUtils.isEmpty(welfareSupplier.getMerchantId())){
                return JsonResult.paramErrWithMsg("请填写接入API的商户号");
            }*/
            welfareSupplier.setIsApi(CommonConstants.NO);
            welfareSupplier.setUpdater(getAdminInfo().getId());
            welfareSupplier.setUpdaterName(getAdminInfo().getUserName());
            welfareSupplier.setCreater(getAdminInfo().getId());
            welfareSupplier.setEnterpriseId(getAdminInfo().getEnterpriseId());
        }
        return JsonResult.success(welfareSupplierService.saveSupplier(welfareSupplier));
    }

    /**
     * 删除供应商
     * @date 2021/5/21 14:31
     * @author caobucao
     * @param id 供应商ID
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @DeleteMapping("deleteSupplier")
    public JsonResult<Integer> deleteSupplier(Integer id) {
        //看供应商是否有已关联商品
        Integer countRelateGoods = welfareSupplierService.selectCountRelateGoodsBySupplierId(id);
        if(countRelateGoods > 0) {
            return JsonResult.paramErrWithMsg("该供应商已关联商品，无法删除！");
        }
        WelfareSupplier welfareSupplier = new WelfareSupplier();
        welfareSupplier.setUpdater(getAdminInfo().getId());
        welfareSupplier.setUpdaterName(getAdminInfo().getUserName());
        welfareSupplier.setUpdateTime(new Date());
        welfareSupplier.setStatus(0);
        welfareSupplier.setId(id);
        Integer count = welfareSupplierService.updateByPrimaryKeySelective(welfareSupplier);
        return count > 0 ? JsonResult.success() : JsonResult.paramErrWithMsg("删除失败！");
    }

    /**
     * 查询供应商下拉框
     * @date 2021/7/16 0016 10:20
     * @author wangjian
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.supplier.pojo.WelfareSupplier>>
     */
    @GetMapping("querySupplierList")
    public JsonResult<List<WelfareSupplier>> querySupplierList() {
        WelfareSupplier welfareSupplier = new WelfareSupplier();
        welfareSupplier.setStatus(CommonConstants.YES);
        return JsonResult.success(welfareSupplierService.select(welfareSupplier));
    }

    /**
     * 查询供应商详情
     * @date 2021/9/22 0022 14:33
     * @author wangjian
     * @param id 供应商ID
     * @return com.dzl.util.JsonResult<com.dzl.supplier.pojo.WelfareSupplier>
     */
    @GetMapping("/querySupplierDetail")
    public JsonResult<WelfareSupplier> querySupplierDetail(Integer id){
        return JsonResult.success(welfareSupplierService.querySupplierDetail(id));
    }

    /**
     * 查询供应商打款账户信息
     * @date 2021/10/14 10:54
     * @author liuguanjun
     * @param supplierId 供应商id
     */
    @GetMapping("querySupplierPaymentinfo")
    public JsonResult<List<WelfareSupplierPaymentinfo>> querySupplierPaymentinfo(Integer supplierId) {
        if(EmptyUtils.isEmpty(supplierId)) {
            return JsonResult.paramErrWithMsg("供应商id[supplierId]不能传空");
        }
        WelfareSupplierPaymentinfo query = new WelfareSupplierPaymentinfo();
        query.setSupplierId(supplierId);
        return JsonResult.success(welfareSupplierPaymentinfoService.select(query));
    }

    /**
     * 开通供应商
     * @date 2021/10/13 14:05
     * @author liuguanjun
     * @param param 供应商id等参数
     */
    @PostMapping("openSupplierAccount")
    public JsonResult<Integer> openSupplierAccount(@RequestBody JSONObject param) {
        Integer id = param.getInteger("id");//供应商id
        if(EmptyUtils.isEmpty(id)) {
            return JsonResult.paramErrWithMsg("传参有误:id必传");
        }
        WelfareSupplier welfareSupplier = new WelfareSupplier();
        welfareSupplier.setId(id);
        welfareSupplier.setUpdateTime(new Date());
        welfareSupplier.setUpdater(getAdminInfo().getId());
        welfareSupplier.setUpdaterName(getAdminInfo().getUserName());
        welfareSupplier.setIsOpen(CommonConstants.YES);
        welfareSupplier.setOpensaasTime(new Date());
        return JsonResult.success(welfareSupplierService.updateByPrimaryKeySelective(welfareSupplier));
    }

    /**
     * 添加供应商账号密码
     * @date 2021/10/13 14:05
     * @author liuguanjun
     * @param param 供应商id,密码等参数
     */
    @PostMapping("addSupplierAccount")
    public JsonResult<Integer> addSupplierAccount(@RequestBody JSONObject param) {
        Integer id = param.getInteger("id");//供应商id
        String password = param.getString("password");//供应商密码
        if(EmptyUtils.isEmpty(id)) {
            return JsonResult.paramErrWithMsg("传参有误:id必传");
        }
        if(EmptyUtils.isEmpty(password)) {
            return JsonResult.paramErrWithMsg("password:[密码]必传");
        }
        if(!NumberUtils.isNumber(password) || password.length()!=6){
            return JsonResult.paramErrWithMsg("密码必须是6位数字");
        }
        WelfareSupplier welfareSupplier = new WelfareSupplier();
        welfareSupplier.setId(id);
        welfareSupplier.setUpdateTime(new Date());
        welfareSupplier.setUpdater(getAdminInfo().getId());
        welfareSupplier.setUpdaterName(getAdminInfo().getUserName());
        welfareSupplier.setPassword(PasswordUtil.AESEncrypt(password));
        return JsonResult.success(welfareSupplierService.updateByPrimaryKeySelective(welfareSupplier));
    }

    /**
     * 批量更改发货信息
     * @date 2021/11/2 14:27
     * @author caobucao
     * @param jsonObject 前端入参
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/batchUpdateSupplierDeliveryInfo")
    public JsonResult<Integer> batchUpdateSupplierDeliveryInfo(@RequestBody JSONObject jsonObject) {
        String ids = jsonObject.getString("ids");
        String deliveryTime = jsonObject.getString("deliveryTime");
        Integer deliveryDays = jsonObject.getInteger("deliveryDays");
        if (EmptyUtils.isEmpty(ids)) {
            return JsonResult.paramErrWithMsg("请选择要更改的供应商");
        }
        if (EmptyUtils.isEmpty(deliveryTime)) {
            return JsonResult.paramErrWithMsg("请选择下单时间");
        }
        if (EmptyUtils.isEmpty(deliveryDays + "")) {
            return JsonResult.paramErrWithMsg("请填写发货时间");
        }
        WelfareSupplierDeliveryDto dto = new WelfareSupplierDeliveryDto();
        List<Integer> idList = Arrays.stream(ids.split(",")).mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());
        dto.setIds(idList);
        dto.setDeliveryTime(deliveryTime);
        dto.setDeliveryDays(deliveryDays);
        dto.setUpdateTime(new Date());
        dto.setUpdaterName(getAdminInfo().getUserName());
        return JsonResult.success(welfareSupplierService.batchUpdateSupplierDeliveryInfo(dto));
    }

    /**
     * 批量更改限制地区
     * @date 2021/11/2 15:53
     * @author caobucao
     * @param jsonObject 前端入参
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/batchUpdateSupplierRestrictedAreas")
    public JsonResult<Integer> batchUpdateSupplierRestrictedAreas(@RequestBody JSONObject jsonObject) {
        String ids = jsonObject.getString("ids");
        String restrictedAreas = jsonObject.getString("restrictedAreas");
        if (EmptyUtils.isEmpty(ids)) {
            return JsonResult.paramErrWithMsg("请选择要更改的供应商");
        }
        WelfareSupplierRestrictedAreasDto dto = new WelfareSupplierRestrictedAreasDto();
        List<Integer> idList = Arrays.stream(ids.split(",")).mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());
        dto.setIds(idList);
        dto.setRestrictedAreas(restrictedAreas);
        dto.setUpdateTime(new Date());
        dto.setUpdaterName(getAdminInfo().getUserName());
        return JsonResult.success(welfareSupplierService.batchUpdateSupplierRestrictedAreas(dto));
    }

}
